package com.example.easyexceldemo.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;

public class MergeService {

    FruitInfo info = new FruitInfo();

    public void mergeFixedCell(){

        String fileName = "mergeFixed" + System.currentTimeMillis() + ".xlsx";

        // 将第一列的数据每隔两行进行合并
        LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
        EasyExcel.write(fileName, DownloadDTO.class).registerWriteHandler(loopMergeStrategy)
                .sheet("sheet名称").doWrite(info.getFruitData());
    }

    public void mergeAbsoluteCell(){
        String fileName = "mergeAbsolute" + System.currentTimeMillis() + ".xlsx";
        OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(1, 3, 1, 1);
        OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy2 = new OnceAbsoluteMergeStrategy(4, 5, 1, 1);
        EasyExcel.write(fileName, DownloadDTO.class)
                .registerWriteHandler(onceAbsoluteMergeStrategy)
                .registerWriteHandler(onceAbsoluteMergeStrategy2)
                .sheet("sheet名称").doWrite(info.getFruitData());
    }

    public void mergeCustomCell(){
        String fileName = "mergeCustom" + System.currentTimeMillis() + ".xlsx";
        MyMergeStrategy myMergeStrategy = new MyMergeStrategy(info.getFruitData(), info.getGroupData());
        EasyExcel.write(fileName, DownloadDTO.class).registerWriteHandler(myMergeStrategy)
                .sheet("sheet名称").doWrite(info.getFruitData());

    }
}
